﻿#include "CMainWnd.h"

#include "search.h"
#include "CEditWnd.h"
#include "CConfigWnd.h"
#include "resource1.h"
#include "myCategoryUiList.h"

MZ_IMPLEMENT_DYNAMIC(MainListWnd) 

MainListWnd::MainListWnd(void)
{
	//初始化编辑模式
	m_opttype = optView;

	//初始化分类
	if (CategorysFunc::countCategorys()<1)
	{
		CategorysFunc::initCategory();
	}
}

MainListWnd::~MainListWnd(void)
{

}

BOOL MainListWnd::OnInitDialog() 
{
	if (!CMzWndEx::OnInitDialog())  
	{
		return FALSE;
	}

	//设置背景图片
	m_PicMainBG.LoadImage(DEFAULT_MAIN_BG,false);
	m_PicMainBG.SetPos(0,0,GetWidth(),GetHeight()-MZM_HEIGHT_TEXT_TOOLBAR);
	m_PicMainBG.SetPaintMode(MZ_PAINTMODE_NORMAL);
	AddUiWin(&m_PicMainBG);

	// 初始化便签列表
	m_List_notes.SetPos(100,0,GetWidth()-100,GetHeight()-MZM_HEIGHT_TEXT_TOOLBAR);
	m_List_notes.SetID(MZ_IDC_LIST_NOTES);
	m_List_notes.EnableScrollBarV(true);
	m_List_notes.EnableNotifyMessage(true);
	m_List_notes.SetItemHeight(110);
	m_List_notes.SetTextColor(RGB(255,0,0));
	m_List_notes.setSelectMode(true);
	AddUiWin(&m_List_notes);

	//搜索按钮
	ImagingHelper* pImg = ImagingHelper::GetImageObject(MzGetInstanceHandle(),IDB_PNG_search,true);
	ImagingHelper* pImg2 = ImagingHelper::GetImageObject(MzGetInstanceHandle(),IDB_PNG_searchb,true);
	m_btnimg.SetID(MZ_IDC_BTN_SEARCH);
	m_btnimg.SetButtonType(MZC_BUTTON_NONE);
	m_btnimg.SetPos(10,6,64,64);
	m_btnimg.SetImage_Normal(pImg);
	m_btnimg.SetImage_Pressed(pImg2);
	m_btnimg.SetMode(UI_BUTTON_IMAGE_MODE_ALWAYS_SHOW_NORMAL);
	m_btnimg.SwapImageZOrder(true);

	AddUiWin(&m_btnimg);

	//分类列表
	m_list_categorys.SetPos(0,70,90,GetHeight()-MZM_HEIGHT_TEXT_TOOLBAR-70);
	m_list_categorys.SetID(MZ_IDC_LIST_CATEGORYS);
	m_list_categorys.EnableScrollBarV(false);
	m_list_categorys.EnableNotifyMessage(true);
	m_list_categorys.SetItemHeight(80);
	m_list_categorys.EnableGridlines(false);
	m_list_categorys.EnableScrollOptimize(true);
	m_list_categorys.setDispName(false);//自定义属性，不显示分类名称，默认为显示
	m_list_categorys.setShowSelect(false);
	m_list_categorys.setSelectMode(false);
	m_list_categorys.setDispPic(true);
	AddUiWin(&m_list_categorys);


	m_toolbar_main.SetPos(0,GetHeight()-MZM_HEIGHT_TEXT_TOOLBAR,GetWidth(),MZM_HEIGHT_TEXT_TOOLBAR);
	m_toolbar_main.SetID(IDC_TOOLBAR_TEXT);
	AddUiWin(&m_toolbar_main);


	switchToolbarMain(optView);
	LoadNotesList(-1);
	LoadCategorysList();

	//结束
	return TRUE;
}

void MainListWnd::switchToolbarMain(EditType optType)
{

	switch(optType)
	{
	case optView:
		{
			m_toolbar_main.SetButton(0,true,true,L"新增");
			m_toolbar_main.SetButton(1,true,true,L"设置");
			m_toolbar_main.SetButton(2,true,true,L"选择"); 
		}
		break;
	case optDelete:
		{
			m_toolbar_main.ShowButton(0,false);
			m_toolbar_main.SetButton(1,true,true,L"删除");
			m_toolbar_main.SetButton(2,true,true,L"完成"); 
		}
		break;

	}
	m_toolbar_main.Invalidate();
	m_toolbar_main.Update();
	return;
}

void MainListWnd::LoadNotesList(int idNO)
{
	this->m_List_notes.RemoveAll();



	char* sql=new char[256];

	if (idNO == -1)//显示全部
	{
		sprintf(sql,"select n.NoteID,n.NoteTitle,substr(n.NoteContext,0,99),n.DateTime from Notes n order by n.NoteID desc");

	} 
	else
	{
		sprintf(sql,"select n.NoteID,n.NoteTitle,substr(n.NoteContext,0,99),n.DateTime  from Notes n where n.CategoryID = %d order by NoteID desc",idNO);

	}

	CppSQLite3DB mydb;
	mydb.open(UnicodeToUTF8(DATABASE_NAME));

	CppSQLite3Query q = mydb.execQuery(sql);

	ListItem li;
	while (!q.eof())
	{
		CMzString strTitle(MAX_TITLE_LENTH+64);
		CMzString strContext(256);
		CMzString strDatetime(MAX_TITLE_LENTH+64);
		int noteid;

		noteid = atoi(q.fieldValue(0));
		wsprintf(strTitle.C_Str(),L"%s",UTF8ToUnicode(q.fieldValue(1)));
		_snwprintf(strContext.C_Str(),256,L"%s",UTF8ToUnicode(q.fieldValue(2)));
		wsprintf(strDatetime.C_Str(), L"%s",UTF8ToUnicode(q.fieldValue(3)));

		/*strContext =strContext.SubStr(0,100);*///内容的首页显示只取100个字符

		MyNotesListData *pmlid = new MyNotesListData;
		pmlid->StringTitle = strTitle;
		pmlid->StringContext = strContext;
		pmlid->StringDatetime = strDatetime;
		pmlid->intNoteID = noteid;
		pmlid->Selected = false;//不选中
		li.Data = pmlid;
		m_List_notes.AddItem(li);

		q.nextRow();
	}
	mydb.close();
	delete []sql;
	// 往列表控件添加项

	m_List_notes.Invalidate();
	m_List_notes.Update();

	m_List_notes.ScrollTo(UI_SCROLLTO_TOP,0,true);
}


void MainListWnd::LoadCategorysList()
{
	this->m_list_categorys.RemoveAll();//删除所有项

	ListItem lj;
	MyCategoryListData *pmlid = new MyCategoryListData;

	//设置一个显示全部内容的分类-1
	pmlid->CategoryID = -1;
	pmlid->strCategoryName = L"全部";
	lj.Data = pmlid;
	m_list_categorys.AddItem(lj);

	char* sql= new char[256];
	sprintf(sql,"select C.CategoryID,C.CategoryPic,C.CategoryName from Categorys C,(select distinct CategoryID from Notes) N where C.CategoryID = N.CategoryID  order by C.CategoryID");

	CppSQLite3DB mydb;
	mydb.open(UnicodeToUTF8(DATABASE_NAME));
	CppSQLite3Query q = mydb.execQuery(sql);

	while (!q.eof())
	{
		CMzString strCategoryPic(MAX_PATH+64);
		CMzString strCategoryName(MAX_PATH+64);
		int cateid;

		MyCategoryListData *pmlid = new MyCategoryListData;

		cateid = atoi(q.fieldValue(0));
		wsprintf(strCategoryPic.C_Str(), L"%s",UTF8ToUnicode(q.fieldValue(1)));
		wsprintf(strCategoryName.C_Str(), L"%s",UTF8ToUnicode(q.fieldValue(2)));

		pmlid->CategoryID = cateid;
		pmlid->strCategoryPic = strCategoryPic;
		pmlid->strCategoryName = strCategoryName;



		lj.Data = pmlid;
		m_list_categorys.AddItem(lj);

		q.nextRow();
	}
	mydb.close();


	delete []sql;


	m_list_categorys.SetSelectedIndex(0);//默认显示全部便签的按钮为选中状态
	m_list_categorys.Invalidate();
	m_list_categorys.Update();
	m_list_categorys.ScrollTo(UI_SCROLLTO_TOP,0,true);
}

LRESULT MainListWnd::MzDefWndProc(UINT message, WPARAM wParam, LPARAM lParam)
{

	switch(message)
	{
	case MZ_WM_MOUSE_NOTIFY:
		{

			UINT_PTR nID = LOWORD(wParam); 
			UINT_PTR nNotify = HIWORD(wParam);
			int x = LOWORD(lParam); 
			int y = HIWORD(lParam); 

			if (nID == MZ_IDC_LIST_NOTES && nNotify == MZ_MN_LBUTTONDOWN )
			{
				if (!m_List_notes.IsMouseDownAtScrolling() && !m_List_notes.IsMouseMoved())
				{
					int nIndex = m_List_notes.CalcIndexOfPos(x, y); 
					m_List_notes.SetSelectedIndex(nIndex); 
					m_List_notes.Invalidate(); 
					m_List_notes.Update();

				}
				return 0;
			}

			if (nID == MZ_IDC_LIST_NOTES && nNotify == MZ_MN_LBUTTONUP && m_opttype == optView)
			{
				if (!m_List_notes.IsMouseDownAtScrolling() && !m_List_notes.IsMouseMoved())
				{
					int nIndex = m_List_notes.CalcIndexOfPos(x, y);
					m_List_notes.SetSelectedIndex(nIndex);  
					MyNotesListData* itemData = (MyNotesListData*)m_List_notes.GetItem(nIndex)->Data;

					NoteEditWnd viewMode(optView,itemData->intNoteID,NULL);
					RECT rcWork = MzGetWorkArea();
					viewMode.Create(rcWork.left,rcWork.top,RECT_WIDTH(rcWork),RECT_HEIGHT(rcWork),m_hWnd, 0, WS_POPUP);
					viewMode.SetAnimateType_Show(MZ_ANIMTYPE_SCROLL_BOTTOM_TO_TOP_2);
					viewMode.SetAnimateType_Hide(MZ_ANIMTYPE_SCROLL_TOP_TO_BOTTOM_1);
					int nRet = viewMode.DoModal();
					if (nRet==ID_OK)
					{
						this->LoadNotesList(-1);
						this->LoadCategorysList();
					}

				}
				return 0;
			}

			if (nID == MZ_IDC_LIST_NOTES && nNotify==MZ_MN_MOUSEMOVE ) 
			{
				if(m_List_notes.GetSelectedIndex()!=-1)
				{
					m_List_notes.SetSelectedIndex(-1);
					m_List_notes.Invalidate();
					m_List_notes.Update();
				}
				return 0;
			}

						if (nID == MZ_IDC_LIST_CATEGORYS && nNotify == MZ_MN_LBUTTONUP )
						{
							if (!m_list_categorys.IsMouseDownAtScrolling() && !m_list_categorys.IsMouseMoved())
							{
								int nIndex = m_list_categorys.CalcIndexOfPos(x, y); 
			
								MyCategoryListData* itemData = (MyCategoryListData*)m_list_categorys.GetItem(nIndex)->Data;
								LoadNotesList(itemData->CategoryID);//载入该分类的所有数据
								m_list_categorys.SetSelectedIndex(nIndex);  
								m_list_categorys.Invalidate();
								m_list_categorys.Update();
			
							}
							return 0;
						}
			
			if (nID == MZ_IDC_LIST_CATEGORYS && nNotify==MZ_MN_MOUSEMOVE ) 
			{
				if(m_list_categorys.GetSelectedIndex()!=-1) 
				{
					m_list_categorys.SetSelectedIndex(-1); 
					m_list_categorys.Invalidate();
					m_list_categorys.Update();   
				}
				return 0;
			}
			if (nID == MZ_IDC_LIST_CATEGORYS && nNotify == MZ_MN_LBUTTONDOWN )
			{
				if (!m_list_categorys.IsMouseDownAtScrolling() && !m_list_categorys.IsMouseMoved())
				{
					int nIndex = m_list_categorys.CalcIndexOfPos(x, y);
					m_list_categorys.SetSelectedIndex(nIndex);  
					m_list_categorys.Invalidate();  
					m_list_categorys.Update(); 

				}
				return 0;
			}
/*
			if (nID == MZ_IDC_LIST_CATEGORYS && wcscmp(m_toolbar_main.GetButtonText(1),L"删除")==0 && nNotify == MZ_MN_LBUTTONDBLCLK)//双击全部选中或不选
			{
				if (!m_list_categorys.IsMouseDownAtScrolling() && !m_list_categorys.IsMouseMoved())
				{
					m_List_notes.setAllSelected(true);
				}
				return 0;
			}*/


		}
	}

	return CMzWndEx::MzDefWndProc(message,wParam,lParam);
}


void MainListWnd::OnMzCommand( WPARAM wParam, LPARAM lParam )
{
	UINT_PTR id = LOWORD(wParam); 
	int nIndex = lParam; 

	switch(id)
	{
	case IDC_TOOLBAR_TEXT:  
		{
			if (nIndex==0) 
			{

				NoteEditWnd addMode(optAdd,0,0);//默认分类为0，即没有选择分类
				RECT rcWork = MzGetWorkArea();
				addMode.Create(rcWork.left,rcWork.top,RECT_WIDTH(rcWork),RECT_HEIGHT(rcWork),m_hWnd, 0, WS_POPUP);
				// 设置窗口切换动画（弹出时的动画）
				addMode.SetAnimateType_Show(MZ_ANIMTYPE_SCROLL_BOTTOM_TO_TOP_2);
				// 设置窗口切换动画（结束时的动画）
				addMode.SetAnimateType_Hide(MZ_ANIMTYPE_SCROLL_TOP_TO_BOTTOM_1);
				int nRet = addMode.DoModal();
				if (nRet==ID_OK)
				{
					this->LoadNotesList(-1);
					this->LoadCategorysList();
				}
				return;

			}
			if (nIndex==1)
			{

				if (wcscmp(m_toolbar_main.GetButtonText(1),L"设置")==0)
				{

					ConfigWnd configMode;
					RECT rcWork = MzGetWorkArea();
					configMode.Create(rcWork.left,rcWork.top,RECT_WIDTH(rcWork),RECT_HEIGHT(rcWork),m_hWnd, 0, WS_POPUP);
					configMode.SetAnimateType_Show(MZ_ANIMTYPE_SCROLL_BOTTOM_TO_TOP_2);
					configMode.SetAnimateType_Hide(MZ_ANIMTYPE_SCROLL_TOP_TO_BOTTOM_1);
					int nRet = configMode.DoModal();
					if (nRet==ID_OK)
					{
						this->LoadNotesList(-1);
						this->LoadCategorysList();
					}
					return;

				}
				else if (wcscmp(m_toolbar_main.GetButtonText(1),L"删除")==0)
				{
					if (m_List_notes.countSelected() == 0)
					{

						MzMessageBoxEx(m_hWnd, L"未选择任何项目！", L"", MB_OK,false);
					} 
					else
					{
						// 弹出菜单
						CPopupMenu ppm;
						struct PopupMenuItemProp pmip;      

						pmip.itemCr = MZC_BUTTON_PELLUCID;
						pmip.itemRetID = IDC_PPM_CANCEL;
						pmip.str = L"取  消";
						ppm.AddItem(pmip);

						pmip.itemCr = MZC_BUTTON_ORANGE;
						pmip.itemRetID = IDC_PPM_OK;
						pmip.str = L"确  定";
						ppm.AddItem(pmip);  

						RECT rc = MzGetWorkArea();      
						rc.top = rc.bottom - ppm.GetHeight();
						ppm.Create(rc.left,rc.top,RECT_WIDTH(rc),RECT_HEIGHT(rc),m_hWnd,0,WS_POPUP);      
						int nID = ppm.DoModal();
						if (nID==IDC_PPM_OK)
						{
							char* delSQL=new char[256];

							CppSQLite3DB mydb;
							mydb.open(UnicodeToUTF8(DATABASE_NAME));

							// 删除选中的列表项
							for (int i=m_List_notes.GetItemCount()-1; i>=0; i--)
							{
								if(m_List_notes.IsSelect(i))
								{
									MyNotesListData* itemData = (MyNotesListData*)m_List_notes.GetItem(i)->Data;//获得数据
									//执行删除操作
									sprintf(delSQL,"delete from Notes where NoteID = %d",itemData->intNoteID);
									mydb.execDML(delSQL);	

								}
							}
							mydb.close();

							delete []delSQL;

							// 刷新列表
							LoadNotesList(-1);
							this->LoadCategorysList();

							m_List_notes.setShowSelect(true);//恢复开启多选按钮
							for (int i=m_List_notes.GetItemCount()-1; i>=0; i--)
							{
								m_List_notes.SelectItem(i, false);
							}
							m_List_notes.Invalidate();
							m_List_notes.Update();



						}

						if (nID==IDC_PPM_CANCEL)
						{
							//所有项目取消选择
							for (int i=m_List_notes.GetItemCount()-1; i>=0; i--)
							{
								m_List_notes.SelectItem(i, false);
							}

							// 刷新列表
							m_List_notes.Invalidate();
							m_List_notes.Update();

						}

					}
				}
				return;
			}
			if (nIndex==2) //选择删除
			{

				if (m_opttype == optView)
				{
					//执行选择
					m_opttype=optDelete;
					switchToolbarMain(optDelete);
					//显示多选按钮
					for (int i=m_List_notes.GetItemCount()-1; i>=0; i--)
					{
						m_List_notes.SelectItem(i, false);
					}
					m_List_notes.setShowSelect(true);
					m_List_notes.Invalidate();
					m_List_notes.Update();

				}
				else if (m_opttype == optDelete)
				{
					//执行取消
					m_opttype=optView;
					switchToolbarMain(optView);
					//所有项目取消选择
					for (int i=m_List_notes.GetItemCount()-1; i>=0; i--)
					{
						m_List_notes.SelectItem(i, false);
					}
					m_List_notes.setShowSelect(false);
					// 刷新列表
					m_List_notes.Invalidate();
					m_List_notes.Update();

				}

				return;
			}

		}
		break;
	case MZ_IDC_BTN_SEARCH:
		{
			SearchWnd SearchNotes;
			RECT rcWork = MzGetWorkArea();
			SearchNotes.Create(rcWork.left,rcWork.top,RECT_WIDTH(rcWork),RECT_HEIGHT(rcWork),m_hWnd, 0, WS_POPUP);
			SearchNotes.SetAnimateType_Show(MZ_ANIMTYPE_SCROLL_BOTTOM_TO_TOP_2);
			SearchNotes.SetAnimateType_Hide(MZ_ANIMTYPE_SCROLL_TOP_TO_BOTTOM_1);
			int nRet = SearchNotes.DoModal();
			if (nRet==ID_OK)
			{
				this->LoadNotesList(-1);
				this->LoadCategorysList();
			}

		}
		break;

	}
}

